﻿#region using
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Markup;
using Microsoft.Win32;
using Microsoft.Surface.Presentation.Generic;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using Microsoft.Surface.Presentation;
using System.Xml;
using System.Globalization;
using System.Linq;
using System.IO;
using System.ComponentModel;
using Ab2d.Common.ReaderWmf;
using MultiTouchDemonstrator;
using System.IO;
using ICSharpCode.SharpZipLib.Tar;
using ICSharpCode.SharpZipLib.GZip;
#endregion

namespace MultiTouchDemonstrator
{
    class ReadVectorialFile
    {
        /// <summary>
        /// Lecture d'un fichier vectoriel et ajout dans la MapArea
        /// </summary>
        /// <param name="fileName">the path to the vectoriel file</param>
        public static Image ReadGeometry(string fileName, MapArea mapArea,Grid mapGrid, EventHandler<ManipulationDeltaEventArgs> map_ManipulationDelta,EventHandler<ManipulationStartingEventArgs> map_ManipulationStarting,EventHandler<ManipulationInertiaStartingEventArgs> map_ManipulationInertiaStarting)
        {
            Ab2d.ReaderWmf myReader;
            Image image;
            WpfXamlWriterSettings xamlWritterSettings;
            string xamlToShow;
            string xamlForTextbox;
            xamlWritterSettings = new WpfXamlWriterSettings();
            myReader = new Ab2d.ReaderWmf();
            myReader.ShowDemoTextInEvaluation = false;
            image = myReader.ReadGeometry(fileName, Ab2d.Common.ReaderWmf.GeometrySettings.FullOptimization);
            xamlForTextbox = myReader.GetXaml(xamlWritterSettings);
            xamlWritterSettings.MaxDataTextLength = -1;
            xamlToShow = myReader.GetXaml(xamlWritterSettings);
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            StringBuilder sb = new StringBuilder();
            sb.Append(xamlToShow);
            using (StreamWriter outfile = new StreamWriter(mydocpath + @"\map.xaml"))
            {
                outfile.Write(sb.ToString());
            }
            Image map = null;
            map = XamlReader.Load(System.Xml.XmlReader.Create(mydocpath + @"\map.xaml")) as Image;
            ImageBrush imageBrush = new ImageBrush(map.Source);
            mapArea.CreateNewCanvas();
            mapArea.MapCanvas.Background = imageBrush;
            mapArea.MapCanvas.ManipulationDelta += map_ManipulationDelta;
            mapArea.MapCanvas.ManipulationStarting += map_ManipulationStarting;
            mapArea.MapCanvas.ManipulationInertiaStarting += map_ManipulationInertiaStarting;
            mapArea.MapCanvas.Height = mapGrid.ActualHeight;
            mapArea.MapCanvas.Width = mapGrid.ActualWidth;
            map.Width = mapArea.MapCanvas.Width;
            return map;
        }
    }
}
